package com.freemyleft.left.zapp.selectimage;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import java.io.File;

/**
 * Created by Administrator on 2017/9/12.
 */

public class PhotoZoomUtils {
    public static  void startPhotoZoom(Activity mActivity, Uri uri, File IconFile, int outputX, int outputY, int activityForResultCode) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0的方式
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(uri, "image/*");
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("scale", true);
                intent.putExtra("outputX", outputX);
                intent.putExtra("outputY", outputY);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,  Uri.fromFile(IconFile));
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                intent.putExtra("noFaceDetection", true); // no face detection
                mActivity.startActivityForResult(intent, activityForResultCode);
            }else {
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setDataAndType(uri, "image/*");
                // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
                intent.putExtra("crop", "true");
                // aspectX aspectY 是宽高的比例
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", outputX);
                intent.putExtra("outputY", outputY);
                intent.putExtra("return-data", false);
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); // 设置输出的格式
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(IconFile));  // 设置输出的地址
                intent.putExtra("noFaceDetection", true); // 不启用人脸识别
                mActivity.startActivityForResult(intent, activityForResultCode);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
